/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Dictator.java
 *
 * Created on Jan 29, 2009, 1:35:27 PM
 */

/**
 *
 * @author Himanshu  Sharma
 */
import java.util.*;
import java.io.*;
import java.awt.event.MouseListener ;
import java.awt.event.MouseEvent ;
import java.awt.event.InputEvent ;


public class Ultimatum extends javax.swing.JFrame {
    int disable, count;
    Random generator;
    Game G;
    BufferedReader din;
    Double money_Won, init_money = 0.0 ;


public Ultimatum(double money) {
    this.init_money = money;
    initComponents();
    l3.setText(Double.toString(init_money));
    G = new Game();
    this.setVisible(true);
    addMouseListener( new Ultimatum.CheckMouse() ) ;
    t1.setText("0.0");
    }

    /** Creates new form Dictator */
    public Ultimatum() {
    this.init_money = 10.0;
    initComponents();
    l3.setText(Double.toString(init_money));
    G = new Game();
    this.setVisible(true);
    addMouseListener( new Ultimatum.CheckMouse() ) ;
    t1.setText("0.0");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        l1 = new javax.swing.JLabel();
        l2 = new javax.swing.JLabel();
        l3 = new javax.swing.JLabel();
        l4 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        l5 = new javax.swing.JLabel();
        b1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        l1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l1.setText("Ultimatum");

        l2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        l2.setText("You have $");

        l4.setText("How much would you like to give to Player 2 ($)");

        l5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        b1.setText("Give");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(318, 318, 318)
                        .addComponent(l1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(262, 262, 262)
                        .addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(l3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(l4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(307, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(321, Short.MAX_VALUE)
                .addComponent(b1)
                .addGap(316, 316, 316))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(249, Short.MAX_VALUE)
                .addComponent(l5, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(230, 230, 230))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(l1)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l2)
                    .addComponent(l3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(l4))
                .addGap(18, 18, 18)
                .addComponent(l5, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(b1)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed
         if(b1.getText().equals("Next")){
                    G.writeLog("Dictator: $ " + money_Won);
                    setVisible(false);
                    G.free=true;

                 }
            

        if (disable ==0) {
                generator = new Random();
                int choice;
                choice = generator.nextInt(3) ;

                if(choice==2){
                    money_Won = 0.0;
                     l2.setText("You lost");
                     l5.setText("Player 2 Rejected the amount");
                     disable =1;
                     b1.setText("Next");

                 }
                 else{
                    money_Won = init_money - Double.parseDouble(t1.getText());
                    l2.setText("You Won $");
                    l5.setText("Player 2 Accepted your offer");
                    l3.setText(Double.toString(money_Won));
                    b1.setText("Next");
                 }
              

              }
}//GEN-LAST:event_b1ActionPerformed

    @Override
    public synchronized void addMouseListener(MouseListener l) {
        super.addMouseListener(l);
    }

    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();

            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                 if(b1.getText().equals("Next")){
                    G.writeLog("Dictator: $ " + money_Won);
                    setVisible(false);
                    G.free=true;

                 }
                double temp = Double.parseDouble(t1.getText()) + 1.0  ;
                t1.setText(Double.toString(temp)) ;

            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {


                if (disable ==0) {
                generator = new Random();
                int choice;
                choice = generator.nextInt(3) ;

                if(choice==2){
                    money_Won = 0.0;
                     l2.setText("You lost");
                     l5.setText("Player 2 Rejected the amount");
                     disable =1;
                     b1.setText("Next");

                 }
                 else{
                    money_Won = init_money - Double.parseDouble(t1.getText());
                    l2.setText("You Won $");
                    l5.setText("Player 2 Accepted your offer");
                    l3.setText(Double.toString(money_Won));
                    b1.setText("Next");
                 }


              }
            }}}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Ultimatum().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b1;
    private javax.swing.JLabel l1;
    private javax.swing.JLabel l2;
    private javax.swing.JLabel l3;
    private javax.swing.JLabel l4;
    private javax.swing.JLabel l5;
    private javax.swing.JTextField t1;
    // End of variables declaration//GEN-END:variables

}
